/**
 * Created by Wilson on 1/23/2015.
 */
(function () {
    angular.module('app')
        .controller('callMapController', callMapController);

    callMapController.$inject = ['$scope', 'mapService', 'markerService', 'imgService', 'alertController'];
    function callMapController($scope, mapService, markerService, imgService, alertController) {

        initMap();

        $scope.addRandomMarker = addRandomMarker;
        $scope.map = '';
        $scope.clearOverlay = clearOverlay;
        $scope.addCarMarker = addCarMarker;
        $scope.moveMarker = moveMarker;
        $scope.choseImgIds = [];
        $scope.wxServerImgIds = [];
        $scope.chooseImg = chooseImg
        $scope.uploadImgs = uploadImgs;
        $scope.pushWxImgIdsToServer = pushWxImgIdsToServer;

        function pushWxImgIdsToServer() {
            imgService.pushWxImgIdsToServer($scope.wxServerImgIds).then(function (data) {
                alertController.simpleAlert(data.errMsg);
            }).catch(function (data) {
                alertController.simpleAlert(data.errMsg);
            });
        }

        function uploadImgs() {
            var ids = $scope.choseImgIds;
            for (var i = 0; i < ids.length; i++) {
                wx.uploadImage({
                    localId: ids[i], // 需要上传的图片的本地ID，由chooseImage接口获得
                    isShowProgressTips: 1, // 默认为1，显示进度提示
                    success: function (res) {
                        wxServerImgIds.push(res.serverId); // 返回图片的服务器端ID
                    }
                });
            }
        }

        function chooseImg() {
            wx.chooseImage({
                success: function (res) {
                    $scope.choseImgIds = res.localIds;
                }
            });
        }

        function moveMarker() {
            markerService.moveMarker(marker, x, y, zoom);
        }

        function clearOverlay() {
            mapService.clearOverlay($scope.map);
        }

        function addRandomMarker() {
            var minX = 116;
            var maxX = 117;

            var x = parseFloat(Math.random() * (maxX - minX + 1) + minX).toFixed(6);

            var minY = 39;
            var maxY = 40;

            var y = parseFloat(Math.random() * (maxY - minY + 1) + minY).toFixed(6);
            var point = new BMap.Point(x, y);
            $scope.map.centerAndZoom(point, 10);
            var marker = new BMap.Marker(point);  // 创建标注
            $scope.map.addOverlay(marker);               // 将标注添加到地图中
            marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
        }

        function addCarMarker() {
            markerService.createCarMarker($scope.map, x, y, zoom);

        }

        function initMap() {
            var map = new BMap.Map("call-map");
            map.centerAndZoom(new BMap.Point(), 14);

            $scope.map = mapService.init('call-map', 116.404, 39.915, 14);

            var point = new BMap.Point(116.417854, 39.921988);

            var marker = new BMap.Marker(point);        // 创建标注

            $scope.map.addOverlay(marker);


        }
    }

})();